package com.fz.us.oa.web.controller.com;

import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.base.bean.jqgrid.JqGridFilter;
import com.fz.us.admin.base.bean.jqgrid.JqGridPager;
import com.fz.us.admin.base.bean.search.order.Order;
import com.fz.us.admin.base.bean.search.result.SearchResultPaging;
import com.fz.us.admin.base.utils.DataUtil;
import com.fz.us.admin.company.bean.FlowEnum;
import com.fz.us.admin.company.entity.Company;
import com.fz.us.admin.company.entity.User;
import com.fz.us.admin.company.service.DutyService;
import com.fz.us.admin.company.service.TaskEntityService;
import com.fz.us.oa.core.entity.com.AllChange;
import com.fz.us.oa.core.service.com.AllChangeService;
import com.fz.us.oa.core.service.com.PersonnelInfoService;
import com.fz.us.oa.web.controller.BaseController;
import com.fz.us.oa.web.shiro.bind.annotation.CurrentUser;
import com.fz.us.oa.web.spring.bind.annotation.RequestJsonParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Created by ZhuChunXiao on 2017/7/17.
 */
@Controller
@RequestMapping(value = "/com/allChange")
public class AllChangeController extends BaseController {
    private static final long serialVersionUID = 6072388105388029868L;

    @Autowired
    private AllChangeService allChangeService;
    @Autowired
    private PersonnelInfoService personnelInfoService;
    @Autowired
    private DutyService dutyService;
    @Autowired
    private TaskEntityService taskEntityService;

    /**
     * 列表
     */
    @RequestMapping(value = "list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> list(@CurrentUser User user,Model model,
                                    String viewtype,
                                    JqGridPager pager,
                                    String parentId,
                                    String checkNum,
                                    @RequestJsonParam(value = "filters", required = false) JqGridFilter filters) {
        Company company = user.getCompany();
        // 排序
        Order order = null;

        SearchResultPaging searchResult = null;
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("company", company);
        params.put("state", BaseEnumManager.StateEnum.Enable);
        if(StringUtils.isNotEmpty(checkNum)){
            params.put("type",checkNum.split(","));
        }
        if(StringUtils.isNotEmpty(parentId)){
            params.put("personnelInfo",personnelInfoService.get(parentId));
        }
        if(StringUtils.isNotEmpty(viewtype)) {
            if (viewtype.equals("1")) {
                //流转中
                params.put("isFinished","0");
                searchResult = allChangeService.paging(null,null,order,
                        "com.fz.us.oa.core.entity.com.AllChange", new FlowEnum.ProcessState []{FlowEnum.ProcessState.Finished},params);
            }else if(viewtype.equals("2")){
                //已完成
                params.put("isFinished","1");
                searchResult = allChangeService.paging(null,null,order,
                        "com.fz.us.oa.core.entity.com.AllChange", new FlowEnum.ProcessState []{FlowEnum.ProcessState.Finished},params);
            }
        }else{
            //所有单子
//            params.put("isFinished",new String[]{"0","1"});
            searchResult = allChangeService.paging(null,null,order,
                    "com.fz.us.oa.core.entity.com.AllChange",
                    new FlowEnum.ProcessState []{FlowEnum.ProcessState.Running, FlowEnum.ProcessState.Backed, FlowEnum.ProcessState.Finished, FlowEnum.ProcessState.Deny}, params);
        }

        List<AllChange> AllChangeList = searchResult.getList();
        ArrayList<Map<String, Object>> dataRows = new ArrayList<Map<String, Object>>();
        Map<String, Object> data = new HashMap<String, Object>();
        Map<String,Object> rMap;
        for(AllChange allChange: AllChangeList){
            rMap = new HashMap<String, Object>();
            rMap.put("id", StringUtils.isEmpty(allChange.getId())?"":allChange.getId());
            rMap.put("name",allChange.getPersonnelInfo().getName());
            int year=Integer.parseInt(allChange.getPersonnelInfo().getCardId().substring(6,10));
            int thisYear=Integer.parseInt(DataUtil.DateToString(new Date(), "yyyy"));
            rMap.put("age",thisYear-year);
            rMap.put("sex",allChange.getPersonnelInfo().getSex()==1?"男":"女");
            rMap.put("department",allChange.getPersonnelInfo().getDepartment().getName());
            rMap.put("post",allChange.getPersonnelInfo().getPost().getName());
            if(allChange.getPersonnelInfo().getNature()==1){
                rMap.put("nature","正式员工");
            }else if(allChange.getPersonnelInfo().getNature()==2){
                rMap.put("nature","试用员工");
            }else{
                rMap.put("nature","暂无");
            }
            if(allChange.getType().equals("1")){
                rMap.put("type","转正申请");
            }else if(allChange.getType().equals("2")){
                rMap.put("type","续约申请");
            }else if(allChange.getType().equals("3")){
                rMap.put("type","离职申请");
            }else if(allChange.getType().equals("4")){
                rMap.put("type","调动申请");
            }else{
                rMap.put("type","未知");
            }
            rMap.put("createdate", DataUtil.DateToString(allChange.getCreateTime()));
            dataRows.add(rMap);
        }
        data.put("dataRows", dataRows);
        data.put("page", searchResult.getPageNumber());
        data.put("rows", searchResult.getPageSize());
        data.put("records", searchResult.getTotalCount());
        data.put("total", searchResult.getPageCount());
        return data;
    }

    /**
     * 从人事信息表中进入异动总汇列表
     */
    @RequestMapping(value = "toList",method = RequestMethod.GET,produces = MediaType.TEXT_HTML_VALUE)
    public String toList(Model model,String parentId){

        model.addAttribute("parentId", parentId);

        return "/main/menu/com/allChange_in";
    }

}
